const dayjs = require("dayjs");

module.exports = {
  getTimeStamp(timeText) {
    let timestamp = 0;

    if (timeText.includes("分钟前")) {
      timestamp = dayjs().subtract(timeText.match(/^(\d+)分钟/)[1], "m").valueOf()
    } else if (timeText.includes("小时前")) {
      timestamp = dayjs().subtract(timeText.match(/^(\d+)小时/)[1], "h").valueOf()
    } else if (timeText.includes("昨天")) {
      timestamp = dayjs().subtract(1, "d").valueOf();
    } else if (timeText.match(/-/g).length === 1) {
      timestamp = dayjs(new Date().getFullYear() + "-" + timeText).valueOf();
    } else if (timeText.match(/-/g).length === 2) {
      timestamp = dayjs(timeText).valueOf();
    } else {
      throw new Error(`时间格式没有可匹配的规则，时间为 ${timeText}`);
    }

    return {
      timestamp,
      timeText,
      diff: dayjs().diff(timestamp, "d"),
    };
  },
  sleep() {
    const delay =  Math.floor(Math.random() * 701 + 300)
    return new Promise(resolve => {
      setTimeout(resolve, delay)
    })
  }
}
